package com.lesama.tcp;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws IOException {
        // 1. 创建ServerSocket对象
        ServerSocket server = new ServerSocket(8989);

        // 2. 获取客户端发来的连接
        System.out.println("服务端就绪，开始接收请求");
        Socket socket = server.accept();

        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();

        byte[] bytes = new byte[1024];
        int len = inputStream.read(bytes);
        String msg = new String(bytes, 0, len);
        String s = "服务端已接收到消息: " + msg;
        System.out.println(s + "来自：" + socket.getInetAddress());
        outputStream.write(s.getBytes());

        socket.close();
        server.close();
    }
}
